const db = require("../db/index");
const moment = require('moment')

// 查询科室
exports.departlists = (req, res) => {
    var sql =
        "select * from depart";
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};

// 查询医生
exports.doctorlists = (req, res) => {
    var sql =
        `select * from doctor where depart_id=${req.query.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};

// 查询排班
exports.datelists = (req, res) => {
    var sql =
        "select * from scheduledate";
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};

// 查询号源数量
exports.schedulecountlists = (req, res) => {
    var sql =
        `select * from schedulecount where date_id=${req.query.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};

// 查询医生信息
exports.doctorinfo = (req, res) => {
    var sql =
        `select * from doctor where depart_id=${req.query.id} and doctor_id =${req.query.doctorID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 查询app医生信息
exports.doctorinfoApp = (req, res) => {
    var sql =
        `select * from doctor where doctor_id =${req.query.doctorID}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};

// 根据id查询日期
exports.seldate = (req, res) => {
    var sql =
        `SELECT * FROM scheduledate where date_id = ${req.query.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};

// 挂号详情列表添加
exports.registerlist = (req, res) => {
    // console.log(req.body.date.split(' ')[1],2222)
    
    const { Medicalcard , Medicalrecord , name , department , doctor , money , status , date , patient_id , timecountid} = req.body
    var sql =
        `INSERT INTO registerlist(Medicalcard,Medicalrecord,name,department,doctor,money,status,date,timestamp,patient_id)VALUES('${Medicalcard}','${Medicalrecord}','${name}','${department}','${doctor}',${money},'${status}','${date}','${moment(new Date()).format("YYYY-MM-DD HH:mm:ss")}',${patient_id})`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            console.log(date.split(' ')[1],2313123)
            var sql =
                `update schedutime set ${date.split(' ')[1] == '8:00-9:00' ? 'timeOne' : date.split(' ')[1] == '09:00-10:00' ? 'timeTwo' : date.split(' ')[1] == '10:00-11:00' ? 'timeThree' : date.split(' ')[1] == '11:00-12:00' ? 'timeFour' : date.split(' ')[1] == '14:00-15:00' ? 'timeFive' : date.split(' ')[1] == '15:00-16:00' ? 'timeSix' : date.split(' ')[1] == '16:00-17:00' ? 'timeSeven' : 'timeEight'} = ${date.split(' ')[1] == '8:00-9:00' ? 'timeOne' : date.split(' ')[1] == '09:00-10:00' ? 'timeTwo' : date.split(' ')[1] == '10:00-11:00' ? 'timeThree' : date.split(' ')[1] == '11:00-12:00' ? 'timeFour' : date.split(' ')[1] == '14:00-15:00' ? 'timeFive' : date.split(' ')[1] == '15:00-16:00' ? 'timeSix' : date.split(' ')[1] == '16:00-17:00' ? 'timeSeven' : 'timeEight'}-1 where TimeCount_id = ${timecountid}`;
            db.query(sql, [], (err, data) => {
                console.log(sql,987655)
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send({
                    status:200,
                    data
                });
            });
        }
    });
};

// 查询挂号信息
exports.registerlistsel = (req, res) => {
    var sql =
        `SELECT * FROM registerlist`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 根据状态查询信息
exports.registerliststa = (req, res) => {
    var sql =
        `SELECT * FROM registerlist WHERE status='${req.query.status}';`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 根据id查询信息
exports.registerlistid = (req, res) => {
    var sql =
        `SELECT * FROM registerlist where register_id=${req.query.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 取消挂号设置状态
exports.registerlistcan = (req, res) => {
    var sql =
        `UPDATE registerlist set status='${req.body.status}' WHERE register_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM registerlist`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
    });
};


// 黑名单管理
exports.blacklist = (req, res) => {
    var sql =
        `SELECT * FROM blacklist`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 添加黑名单
exports.blacklistinsert = (req, res) => {
    const { Medicalcard , Idcard , name } = req.body
    var sql =
        `INSERT INTO blacklist(Medicalcard,Idcard,name,brokentimes,status) VALUES('${Medicalcard}','${Idcard}','${name}',1,'未限制')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM blacklist`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
    });
};

// 移除黑名单
exports.blacklistdel = (req, res) => {
    req.body.selectedRows.forEach(item=>{
        var sql =
            `DELETE FROM blacklist WHERE blacklist_id=${item.blacklist_id}`;
        db.query(sql, [], (err, data) => {
            if (err) {
                console.log("出错了", err);
                return res.send("错误" + err.message);
            }
        });
    })
    var sql =
        `SELECT * FROM blacklist`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);registerlist
        }
        res.send(data);
    });
};
// 搜索
exports.blacklistserach = (req, res) => {
    const { Medicalcard ,  IDcard , name , status } = req.query
    var sql =
        `SELECT * FROM blacklist where Medicalcard='${Medicalcard}' and Idcard='${IDcard}' and name='${name}' ${status=='全部状态'?'':`and status="${status}"`}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};